package com.zby.lease.web.admin.service.imp;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zby.lease.model.pojo.*;
import com.zby.lease.web.admin.mapper.*;
import com.zby.lease.web.admin.service.LeaseAgreementService;
import com.zby.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.zby.lease.web.admin.vo.agreement.AgreementVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;


    @Override
    public IPage<AgreementVo> queryAgreementPage(IPage<AgreementVo> page, AgreementQueryVo queryVo) {

        return leaseAgreementMapper.queryAgreementPage(page,queryVo);

    }

    @Override
    public AgreementVo getAgreementById(Long id) {
        //1.查询租约信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);

        //2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(leaseAgreement.getApartmentId());

        //3.查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(leaseAgreement.getRoomId());

        //4.查询支付方式
        PaymentType paymentType = paymentTypeMapper.selectById(leaseAgreement.getPaymentTypeId());

        //5.查询租期
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseAgreement.getLeaseTermId());
        // 创建一个AgreementVo对象，用于展示租赁协议的详细信息
        AgreementVo agreementVo = new AgreementVo();

        // 使用BeanUtils工具类将leaseAgreement对象的属性复制到agreementVo对象中
        // 这里解释了为什么使用工具类进行属性复制：提高代码复用性和简化对象属性的复制过程
        BeanUtils.copyProperties(leaseAgreement, agreementVo);

        // 设置协议中的公寓信息
        agreementVo.setApartmentInfo(apartmentInfo);
        // 设置协议中的房间信息
        agreementVo.setRoomInfo(roomInfo);
        // 设置协议中的付款方式
        agreementVo.setPaymentType(paymentType);
        // 设置协议中的租赁期限
        agreementVo.setLeaseTerm(leaseTerm);
        return agreementVo;
    }
}




